<template>
    <div class="pub-box">
        <!-- 发布公告 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/loginRedirect' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>公告管理</el-breadcrumb-item>
            <el-breadcrumb-item>发布公告</el-breadcrumb-item>
        </el-breadcrumb>
        <el-form :model="noticeForm" :rules="noteRules" ref="noteRuleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="公告标题" prop="noteTitle">
                <el-input v-model="noticeForm.noteTitle" placeholder="请输入标题" class="note-input"></el-input>
            </el-form-item>
            <el-form-item label="公告内容" prop="noteDetail">
                <el-input v-model="noticeForm.noteDetail" type="textarea" placeholder="请输入公告内容" class="note-input"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="publishNotice('noteRuleForm')">点击发布</el-button>
                <el-button type="warning" @click="Reset('noteRuleForm')">点击重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data(){
        return{
            noticeForm:{},
            noteRules:{
                noteTitle:[
                    { required: true, message: '请输入公告标题', trigger: 'blur' },
                    { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
                ],
                noteDetail:[
                    { required: true, message: '请输入公告内容', trigger: 'blur' },
                    { min: 5, max: 150, message: '长度在 5 到 150 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        //发布公告
        publishNotice(noteRuleForm){
            this.$refs[noteRuleForm].validate((valid) => {
                if (valid) {
                    this.$http.post('/noticeInfo/publishNotice',this.noticeForm).then((res)=>{
                        if(res.code === 200 && res.data){
                            this.$message.success("发布成功!!!");
                            this.noticeForm = {};
                        }else{
                            this.$message.warning(res.data.msg);
                        }
                    }).catch((err=>{
                        this.$message.error("发布失败,请联系管理员!!!");
                    }));
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        //重置
        Reset(noteRuleForm){
            this.$refs[noteRuleForm].resetFields();
        }
    }
}
</script>

<style scoped>
.pub-box .demo-ruleForm{
    margin-top: 20px;
}
</style>
